﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Isha.Data.Infrastructure;
using Isha.Infrastructure;
using Isha.Services;
using Isha.Repositories;
using Isha.Repositories.Infrastructure;
using System.Web.Security;

namespace Isha
{
    public class IshaBuilder
    {

        private static IUnityContainer _container;

        public static IUnityContainer Container
        {
            get
            {
                if (_container == null)
                    Build();

                return _container;
            }
        }

        public IshaBuilder()
        {

        }

        public static void Build()
        {

            IUnityContainer container = new UnityContainer();

            _container = container;

            container.RegisterInstance<IUnityContainer>(container);

            // Database
            container
                .RegisterType<IDatabaseFactory, DatabaseFactory>(new HttpContextLifetimeManager<IDatabaseFactory>())
                .RegisterType<IUnitOfWork, UnitOfWork>(new HttpContextLifetimeManager<IUnitOfWork>());

            // Repositories
            container
                .RegisterType<IUserRepository, UserRepository>(new HttpContextLifetimeManager<IUserRepository>())
                .RegisterType<IRoleRepository, RoleRepository>(new HttpContextLifetimeManager<IRoleRepository>())
                .RegisterType<ILanguageRepository, LanguageRepository>(new HttpContextLifetimeManager<ILanguageRepository>())
                .RegisterType<IContentTypeRepository, ContentTypeRepository>(new HttpContextLifetimeManager<IContentTypeRepository>())
                .RegisterType<IContentItemRepository, ContentItemRepository>(new HttpContextLifetimeManager<IContentItemRepository>())
                .RegisterType<ICommentRepository, CommentRepository>(new HttpContextLifetimeManager<ICommentRepository>())
                .RegisterType<IPartRepository, PartRepository>(new HttpContextLifetimeManager<IPartRepository>());

            // Services
            container
                .RegisterType<IFormsAuthenticationService, FormsAuthenticationService>()
                .RegisterType<IMembershipService, AccountMembershipService>()
                .RegisterInstance<MembershipProvider>(Membership.Provider)
                .RegisterType<ISecurityService, SecurityService>(new HttpContextLifetimeManager<ISecurityService>());

            container
                .RegisterType<ILanguageService, LanguageService>(new HttpContextLifetimeManager<ILanguageService>())
                .RegisterType<IContentTypeService, ContentTypeService>(new HttpContextLifetimeManager<IContentTypeService>())
                .RegisterType<IContentItemService, ContentItemService>(new HttpContextLifetimeManager<IContentItemService>())
                .RegisterType<IPartService, PartService>(new HttpContextLifetimeManager<IPartService>());
        }
    }
}
